const CDBSave = require('./dbsave');

class Get_User extends CDBSave.CDBSave{
    constructor(){
        super();
    }

    filePath(){
        return 'paotui_user.json'
    }

    load(data){
        if(!data)return;

        this.notices = data;
    }


    save(){
        return this.notices;
    }

    getUser(number,password){
        let temp = null;
        for(let i=0;i<this.notices.length;i++){
            if(this.notices[i].student_number == number && this.notices[i].secret == password){
                temp = this.notices[i];
            }else{
                continue;
            }
        }
        return temp;
    }

    revise_ifRunner(number,info){
        for(let i=0;i<this.notices.length;i++){
            if(this.notices[i].student_number == number && this.notices[i].Real_name == info.Re_name){
                this.notices[i].if_runner = 1;
                this.notices[i].ID = info.ID;
                return this.notices[i];
            }
        }
        return -1;
    }


    register_User(New_user){
        let id = this.notices.length + 1;
        New_user.id = id;
        let Add_user = new User_child(New_user);
        this.notices.push(Add_user);
    }


    revise_message(name,number){
        for(let i=0;i<this.notices.length;i++){
            if(this.notices[i].student_number == number){
                this.notices[i].Net_name = name;
                break;
            }
        }
    }


    //修改密码
    revise_secret(old,secret,number){
        // console.log(old,secret);
        let flag = 0;
        for(let i =0;i<this.notices.length;i++){
            if(this.notices[i].secret == old && this.notices[i].student_number == number){
                if(this.notices[i].secret == secret){
                    flag = 2;
                    break;
                }
                this.notices[i].secret = secret;
                flag = 1;
                break;
            }
        }

        if(flag == 0){
            return -1;
        }
        else if(flag == 2){
            return 1;
        }
        else{
            return 0;
        }
    }
}



class User_child{
    constructor(message){
        this.load(message)
    }

    load(data){
        this.id = data.id;
        this.student_number = data.New_number;
        this.Net_name = data.Int_name;
        this.secret = data.New_password;
        this.Real_name = data.name;
        this.ID = "";
        this.if_runner = 0
    }

    save(){
        return{
            id:this.id,
            student_number:this.student_number,
            Net_name:this.Net_name,
            secret:this.secret,
            Real_name:this.Real_name,
            ID:this.ID,
            if_runner:this.if_runner
        }
    }
}



module.exports = {Get_User,User_child}